<template>
  <Dialog
    :title="t('doubleChioce.index.chooseTitle')"
    v-model="dialogVisible"
    width="400"
    :fullscreen="false"
  >
    <el-select
      v-model="choiceData.useQuota"
      :placeholder="t('common.selectTextPlaceholder') + t('batchMentor.fields.quota')"
      style="width: 100%"
      v-loading="formLoading || choiceInfoLoading"
    >
      <el-option
        v-if="choice.quota1 > 0"
        value="保障性指标"
        :disabled="choice.quota1Remaining === 0"
      >
        <span style="float: left">{{ t('batchMentor.fields.quota1') }}</span>
        <span style="float: right; color: var(--el-text-color-secondary); font-size: 13px">
          {{ t('doubleChioce.index.remaining', { x: choice.quota1Remaining }) }}
        </span>
      </el-option>
      <el-option
        v-if="choice.quota2 > 0"
        value="竞争性指标"
        :disabled="choice.quota2Remaining === 0"
      >
        <span style="float: left">{{ t('batchMentor.fields.quota2') }}</span>
        <span style="float: right; color: var(--el-text-color-secondary); font-size: 13px">
          {{ t('doubleChioce.index.remaining', { x: choice.quota2Remaining }) }}
        </span>
      </el-option>
      <el-option
        v-if="choice.quota3 > 0"
        value="奖励性指标"
        :disabled="choice.quota3Remaining === 0"
      >
        <span style="float: left">{{ t('batchMentor.fields.quota3') }}</span>
        <span style="float: right; color: var(--el-text-color-secondary); font-size: 13px">
          {{ t('doubleChioce.index.remaining', { x: choice.quota3Remaining }) }}
        </span>
      </el-option>
      <el-option v-if="choice.quota4 > 0" value="博士指标" :disabled="choice.quota4Remaining === 0">
        <span style="float: left">{{ t('batchMentor.fields.quota4') }}</span>
        <span style="float: right; color: var(--el-text-color-secondary); font-size: 13px">
          {{ t('doubleChioce.index.remaining', { x: choice.quota4Remaining }) }}
        </span>
      </el-option>
      <el-option
        v-if="choice.quota5 > 0"
        value="优秀学生奖励指标"
        :disabled="!student.is985 || choice.quota5Remaining === 0"
      >
        <span style="float: left">{{ t('batchMentor.fields.quota5') }}</span>
        <span style="float: right; color: var(--el-text-color-secondary); font-size: 13px">
          {{ t('doubleChioce.index.remaining', { x: choice.quota5Remaining }) }}
        </span>
      </el-option>
    </el-select>
    <template #footer>
      <el-button @click="handleSubmit" type="primary" :disabled="formLoading"
        >{{ t('common.ok') }}
      </el-button>
      <el-button @click="dialogVisible = false">{{ t('common.cancel') }}</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import {
  DoubleChioceApi,
  DoubleChioceMyBatchInfoRespVO,
  DoubleChioceVO
} from '@/api/recruit/doublechioce'
import { StudentApi, StudentVO } from '@/api/recruit/recruitstudent'

defineOptions({ name: 'MentorQuotaSelect' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false)

const choiceInfoLoading = ref(false)
const choice = ref<DoubleChioceMyBatchInfoRespVO>({} as DoubleChioceMyBatchInfoRespVO)
const getChoiceInfo = async () => {
  choiceInfoLoading.value = true
  try {
    if (choiceData.value.batchId) {
      choice.value = await DoubleChioceApi.myBatchInfo(choiceData.value.batchId)
    }
  } finally {
    choiceInfoLoading.value = false
  }
}

const student = ref<StudentVO>({} as StudentVO)
const getStudentInfo = async () => {
  formLoading.value = true
  try {
    if (choiceData.value.recruitStudentId) {
      student.value = await StudentApi.getStudent(choiceData.value.recruitStudentId)
    }
  } finally {
    formLoading.value = false
  }
}

const choiceData = ref<Partial<DoubleChioceVO>>({})
// 只需要batchId和recruitStudentId
const open = (choice: Partial<DoubleChioceVO>) => {
  choiceData.value = choice
  dialogVisible.value = true
  formLoading.value = false
  getChoiceInfo()
  getStudentInfo()
}

const handleSubmit = async () => {
  if (!choiceData.value.useQuota) {
    message.error(t('common.selectTextPlaceholder') + t('batchMentor.fields.quota'))
    return
  }

  try {
    await message.confirm(t('doubleChioce.mentor.confirmSelect'))
  } catch {
    return
  }

  try {
    formLoading.value = true
    await DoubleChioceApi.mentorDoubleChioce({
      batchId: choiceData.value.batchId,
      recruitStudentId: choiceData.value.recruitStudentId,
      useQuota: choiceData.value.useQuota
    })
    message.success(t('common.operSuccess'))
    dialogVisible.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

defineExpose({
  open
})
</script>
